/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package IpChat_For_Testing;

/**
 *
 * @author lubinchen
 */
import java.net.*;
import java.io.*;

public class ChatClientThread extends Thread
{  private Socket           socket   = null;
   private ChatClient       client   = null;
   private DataInputStream  streamIn = null;
   private String cn="noNameFound";

   public ChatClientThread(ChatClient _client, Socket _socket, String clientName)
   {  client   = _client;
      socket   = _socket;
       cn=clientName;
      open();
      start();
   }

   public void open()
   {  try
      {  streamIn  = new DataInputStream(socket.getInputStream());      
      }
      catch(IOException ioe)
      {  System.out.println("Error getting input stream: " + ioe);
         client.stop();
      }
   }

   public void close()
   {  try
      {  if (streamIn != null) streamIn.close();
      }
      catch(IOException ioe)
      {  System.out.println("Error closing input stream: " + ioe);
      }
   }


   public void run()
   {
       System.out.println("Client thread for "+cn+" running");
       while (true)
      {  try

         {  System.out.println("@ChatClientThread.run():  Someinput reveiced= " +streamIn.readUTF());
             client.handle(streamIn.readUTF());
         }
         catch(IOException ioe)
         {  System.out.println("@ChatClientThread.run():  Listening error from client thread: " + ioe.getMessage());
            client.stop();
         }
      }
      
   }



}